package cn.nebula.music.service;

import java.sql.Timestamp;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.nebula.music.dao.CommentDAO;
import cn.nebula.music.domain.Comment;

@Service
public class CommentServiceImpl implements CommentService {

	@Autowired
	private CommentDAO commentDAO;

	@Override
	public Set<Comment> findAllComments() throws Exception {
		return commentDAO.findAllComments();
	}

	@Override
	public Set<Comment> findAllCommentsOfBT() throws Exception {
		return commentDAO.findAllCommentsOfBT();
	}

	@Override
	public Comment findCommentById(Integer id) throws Exception {
		return commentDAO.findCommentById(id);
	}

	@Override
	public Set<Comment> findCommentByContent(String content) throws Exception {
		return commentDAO.findCommentByContent(content);
	}

	@Override
	public Set<Comment> findCommentByTime(Timestamp time) throws Exception {
		return commentDAO.findCommentByTime(time);
	}

	@Override
	public Set<Comment> findCommentsBySong(Integer songId) throws Exception {
		return commentDAO.findCommentsBySong(songId);
	}

	@Override
	public Set<Comment> findCommentsByUser(Integer userId) throws Exception {
		return commentDAO.findCommentsByUser(userId);
	}

	@Override
	public Set<Comment> findCommentByComment(Integer commentId) throws Exception {
		return commentDAO.findCommentByComment(commentId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void addComment(Comment comment) throws Exception {
		commentDAO.addComment(comment);
		for (Comment comment2 : comment.getComments()) {
			Comment comment3 = commentDAO.findCommentById(comment2.getId());
			comment3.setComment(comment);
			commentDAO.updateComment(comment3);
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateComment(Comment comment) throws Exception {
		commentDAO.updateComment(comment);
		Boolean isModify = false;
		int size1 = 0;// 更改前的数量
		int size2 = 0;// 更改后的数量
		Set<Comment> comments1 = findCommentById(comment.getId()).getComments();
		Set<Comment> comments2 = comment.getComments();
		if (comments1 != null) {
			size1 = comments1.size();
		}
		if (comments2 != null) {
			size2 = comments2.size();
		}
		if (size1 == size2) {
			if (!comments1.toString().equals(comments2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Comment comment1 : comments1) {
				Comment comment2 = commentDAO.findCommentById(comment1.getId());
				comment2.setComment(null);
				commentDAO.updateComment(comment2);
			}
			for (Comment comment1 : comments2) {
				Comment comment2 = commentDAO.findCommentById(comment1.getId());
				comment2.setComment(comment);
				commentDAO.updateComment(comment2);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteCommentById(Integer id) throws Exception {
		for (Comment comment : commentDAO.findAllComments()) {
			if (comment.getComment() != null && comment.getComment().getId().equals(id)) {
				comment.setComment(null);
				commentDAO.updateComment(comment);
			}
		}
		commentDAO.deleteCommentById(id);
	}

}
